package drone.iqgc.controller ;

import java.util.LinkedHashMap ;
import java.util.Map ;
import java.util.Objects ;

import javax.annotation.Resource ;

import org.springframework.stereotype.Controller ;
import org.springframework.web.bind.annotation.RequestBody ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.ResponseBody ;

import com.alibaba.fastjson.JSON ;

import drone.basis.GlobalConfig ;
import drone.basis.base.BaseController ;
import drone.basis.base.BaseResult ;
import drone.basis.dictionary.MissionDict ;
import drone.basis.dictionary.StateDict ;
import drone.basis.util.PageUtil ;
import drone.basis.util.StringUtil ;
import drone.facade.model.entity.core.CoreMissionEntity ;
import drone.iqgc.service.entity.core.CoreMissionService ;

@Controller
@RequestMapping ( "mission" )
public class MissionController extends BaseController
{

	@Resource
	private CoreMissionService missionService ;

	public MissionController ( )
	{
		super ( "mission" ) ;
	}

	@RequestMapping ( "complate" )
	@ResponseBody
	public String missionComplate (
			String misNames
	) throws Exception
	{

		Map < String , String > paramMap = new LinkedHashMap < String , String > ( ) ;
		paramMap.put ( "misNames" , misNames ) ;

		PageUtil pageUtil = new PageUtil ( paramMap.get ( "pageIndex" ) , paramMap.get ( "pageSize" ) ) ;

		BaseResult < CoreMissionEntity > baseResult = missionService.search ( CoreMissionEntity.CoreMissionField.misName.name ( ) , new Boolean ( true ) , pageUtil , paramMap ) ;

		return JSON.toJSONString ( baseResult.getResultList ( ) ) ;

	}

	@RequestMapping ( "grab" )
	@ResponseBody
	public CoreMissionEntity missionGrab (
			@RequestBody ( required = false ) Map < String , String > paramMap
	) throws Exception
	{

		CoreMissionEntity missionBean = null ;

		if ( ! Objects.isNull ( paramMap ) && paramMap.containsKey ( "misNo" ) )
		{
			missionBean = missionService.fetch ( paramMap.get ( "misNo" ).toString ( ) ) ;
		}

		if ( Objects.isNull ( missionBean ) )
		{

			missionBean = new CoreMissionEntity ( ) ;
			missionBean.setMisNo ( StringUtil.guid ( ) ) ;
			missionBean.setMisType ( MissionDict.STANDARD.name ( ) ) ;
			missionBean.setMisData ( "[]" ) ;
		}

		return missionBean ;

	}

	@RequestMapping ( "write" )
	@ResponseBody
	public BaseResult < CoreMissionEntity > missionWrite (
			@RequestBody ( required = true ) CoreMissionEntity missionBean
	) throws Exception
	{

		try
		{
			missionBean = missionService.persist ( missionBean ) ;
		} catch ( Exception e )
		{
			return new BaseResult.Builder < CoreMissionEntity > ( ).codeInfo ( StateDict.NO.getCode ( ) ).messageInfo ( "添加失败 ,出现异常 : " + e.getMessage ( ) ).build ( ) ;
		}

		return new BaseResult.Builder < CoreMissionEntity > ( ).codeInfo ( StateDict.YES.getCode ( ) ).messageInfo ( "操作成功" ).resultBean ( missionBean ).build ( ) ;
	}

	@RequestMapping ( "paste" )
	@ResponseBody
	public BaseResult < CoreMissionEntity > missionPaste (
			@RequestBody ( required = false ) Map < String , String > paramMap
	) throws Exception
	{

		Integer resultCount = new Integer ( 0 ) ;

		if ( ! Objects.isNull ( paramMap ) && ( paramMap.containsKey ( "misNo" ) || paramMap.containsKey ( "misNos" ) ) )
		{
			if ( ! StringUtil.isEmpty ( paramMap.get ( "misNo" ) ) )
			{
				CoreMissionEntity missionBean = missionService.fetch ( paramMap.get ( "misNo" ) ) ;
				missionBean.setMisNo ( StringUtil.guid ( ) ) ;

				String subfix = "_COPY_" + StringUtil.number ( 4 ) ;
				if ( missionBean.getMisName ( ).indexOf ( "_COPY" ) >= 0 )
				{
					missionBean.setMisName ( missionBean.getMisName ( ).substring ( 0 , missionBean.getMisName ( ).indexOf ( "_COPY" ) ) + subfix ) ;
				} else
				{
					missionBean.setMisName ( missionBean.getMisName ( ) + subfix ) ;
				}

				missionService.persist ( missionBean ) ;
				resultCount = 1 ;
			}
		}

		BaseResult < CoreMissionEntity > baseResult = new BaseResult < CoreMissionEntity > ( resultCount.longValue ( ) ) ;
		return baseResult ;

	}

	@RequestMapping ( "erase" )
	@ResponseBody
	public BaseResult < CoreMissionEntity > missionErase (
			@RequestBody ( required = false ) Map < String , String > paramMap
	) throws Exception
	{

		Integer resultCount = new Integer ( 0 ) ;

		if ( ! Objects.isNull ( paramMap ) && ( paramMap.containsKey ( "misNo" ) || paramMap.containsKey ( "misNos" ) ) )
		{
			if ( ! StringUtil.isEmpty ( paramMap.get ( "misNo" ) ) )
			{
				resultCount = missionService.remove ( paramMap.get ( "misNo" ) ) ;
			}
			if ( ! StringUtil.isEmpty ( paramMap.get ( "misNos" ) ) )
			{
				resultCount = missionService.expunge ( paramMap ) ;
			}

		}

		BaseResult < CoreMissionEntity > baseResult = new BaseResult <> ( resultCount.longValue ( ) ) ;
		return baseResult ;

	}

	@RequestMapping ( "collect" )
	@ResponseBody
	public BaseResult < CoreMissionEntity > missionCollect (
			@RequestBody ( required = false ) Map < String , String > paramMap
	) throws Exception
	{

		PageUtil pageUtil = new PageUtil ( paramMap.get ( "pageIndex" ) , paramMap.get ( "pageSize" ) ) ;
		String sortField = StringUtil.space ( paramMap.get ( "sortField" ) ) ;
		Boolean sortOrder = Objects.isNull ( paramMap.get ( "sortOrder" ) ) ? new Boolean ( false ) : new Boolean ( paramMap.get ( "sortOrder" ).toString ( ) ) ;

		BaseResult < CoreMissionEntity > baseResult = missionService.search ( sortField , sortOrder , pageUtil , paramMap ) ;

		return baseResult ;

	}

	@RequestMapping ( "export" )
	@ResponseBody
	public BaseResult < CoreMissionEntity > missionExport (
			@RequestBody ( required = false ) Map < String , String > paramMap
	) throws Exception
	{

		PageUtil pageUtil = new PageUtil ( 1 , GlobalConfig.EXPORT.intValue ( ) ) ;
		String sortField = StringUtil.space ( paramMap.get ( "sortField" ) ) ;
		Boolean sortOrder = Objects.isNull ( paramMap.get ( "sortOrder" ) ) ? new Boolean ( false ) : new Boolean ( paramMap.get ( "sortOrder" ).toString ( ) ) ;

		BaseResult < CoreMissionEntity > baseResult = missionService.search ( sortField , sortOrder , pageUtil , paramMap ) ;

		return baseResult ;
	}

}
